home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group02b.txt / 000069_icon-group-sender_Tue Oct 8 13:13:31 2002.msg < prev    next >
Internet Message Format  |  2003-01-02  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.11.1/8.11.1) id g98KCPY27292
  4.     for icon-group-addresses; Tue, 8 Oct 2002 13:12:25 -0700 (MST)
  5. Message-Id: <200210082012.g98KCPY27292@baskerville.CS.Arizona.EDU>
  6. From: "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net>
  7. X-Newsgroups: comp.lang.icon
  8. Subject: A Better Reverse / Map Example
  9. X-Priority: 3
  10. X-MSMail-Priority: Normal
  11. X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
  12. X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
  13. Date: Tue, 08 Oct 2002 18:20:22 GMT
  14. X-Complaints-To: abuse@verizon.net
  15. To: icon-group@cs.arizona.edu
  16. Errors-To: icon-group-errors@cs.arizona.edu
  17. Status: RO
  18.  
  19. The book "The Icon Programming Language" has an important chapter called
  20. "Mappings and Labelings". The major thrust of the chapter is that the
  21. predefined map function can quickly perform a variety of transpositions. To
  22. illustrate, an example of how map could be used to implement the reverse
  23. function as a procedure.
  24.  
  25. One of the limitations of the map technique is that the number of characters
  26. that can be transported is limited by the number of 8-bit characters, namely
  27. 256. The procedure version of reverse must therefore break up strings longer
  28. than 256 into pieces of at most 256 characters, reverse them, and put them
  29. together.
  30.  
  31. In the example in the book, this is done by recursion. This use of recursion
  32. strikes me as somewhat awkward. As an alternative approach, I would
  33. decompose long strings into 256 character pieces using reverse string
  34. scanning, i.e. we scan the string, and start the scanning by moving to the
  35. right end of the string. We then use move with a negative argument to chop
  36. the string into pieces small enough to map. The technique is illustrated
  37. here:
  38.  
  39. ############################################################################
  40. ####
  41.  
  42. procedure reverse(s)
  43.     local result
  44.     static trans, labels, max
  45.  
  46.     initial {
  47.         # Set up labels, trans, max for short reverse mappings
  48.         labels := "ab"
  49.         trans  := "ba"
  50.         max    := *labels
  51.  
  52.         # use this to bootstrap to 256 character mappings
  53.         trans  := reverse( result := string(&cset) )
  54.         labels := result
  55.         max    := *labels
  56.         }
  57.  
  58.     s ? {
  59.         tab( 0 )
  60.  
  61.         result := ""
  62.         while result ||:= map( trans, labels, move( -max ) )
  63.         if ( s := tab( 1 ) ) >> "" then
  64.             result ||:= map( right( trans, *s ), left ( labels, *s ), s )
  65.         }
  66.  
  67.     return result
  68.  
  69. end
  70.  
  71.  
  72.